package vip.specimen.system.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.specimen.system.common.enums.ResultCode;
import vip.specimen.system.common.handler.SystemExceptionHandler;
import vip.specimen.system.datasource.entity.SystemResourceKindEntity;
import vip.specimen.system.datasource.entity.SystemResourceSuffixEntity;
import vip.specimen.system.repository.SystemResourceSuffixRepository;
import vip.specimen.system.datasource.validation.SystemResourceSuffixValidation;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * *@Author: 余浪人
 * *@Date：2021/3/10 15:02
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Service
public class SystemResourceSuffixService {
    @Resource
    private SystemResourceSuffixRepository resourceSuffixRepository;

    /**
     * 插入数据
     *
     * @param suffixValidation 验证
     * @return 数据
     */
    public SystemResourceSuffixEntity insert(SystemResourceSuffixValidation suffixValidation) {
        return this.resourceSuffixRepository.save(new SystemResourceSuffixEntity(suffixValidation.getSuffix().toLowerCase()));
    }

    /**
     * 根据后缀名查询
     *
     * @param suffix 文件后缀
     * @return object
     */
    public SystemResourceSuffixEntity findBySuffix(String suffix) {
        return this.resourceSuffixRepository.findSystemResourceSuffixEntityBySuffix(suffix.toLowerCase());
    }

    /**
     * 获取全部后缀
     *
     * @return 数据集
     */
    public List<SystemResourceSuffixEntity> findAll() {
        return this.resourceSuffixRepository.findAll();
    }

    /**
     * 根据uuid 更改状态
     *
     * @param uuid uuid
     * @return 数据
     */
    @Transactional
    public SystemResourceSuffixEntity update(UUID uuid) {
        if (this.resourceSuffixRepository.existsById(uuid)) {
            SystemResourceSuffixEntity suffixEntity = this.resourceSuffixRepository.findById(uuid).get();
            suffixEntity.setEnable(!suffixEntity.isEnable());
            suffixEntity.setUpdateTime(Calendar.getInstance().getTime());
            return this.resourceSuffixRepository.save(suffixEntity);
        }
        return null;
    }

    /**
     * 根据uuid => 查询
     *
     * @param uuids list
     * @return List
     */
    public List<SystemResourceSuffixEntity> findAllById(Set<UUID> uuids) {
        return this.resourceSuffixRepository.findAllById(uuids);
    }


    /**
     * 根据kind（分类）批量修改后缀的所属分类
     *
     * @param suffixList 后缀列表
     * @param kindEntity 分类
     */
    @Transactional
    public void update(List<SystemResourceSuffixEntity> suffixList, SystemResourceKindEntity kindEntity) {
        this.updateKindNull(this.resourceSuffixRepository.findSystemResourceSuffixEntityByKindEntityOrderByCreateTimeDesc(kindEntity));
        for (SystemResourceSuffixEntity suffix : suffixList) {// 设置新的后缀
            suffix.setKindEntity(kindEntity);
            this.resourceSuffixRepository.save(suffix);
        }
    }

    /**
     * 批量修改 后缀所属分类
     *
     * @param suffixEntities 后缀列表
     */
    public void updateKindNull(List<SystemResourceSuffixEntity> suffixEntities) {
        for (SystemResourceSuffixEntity k : suffixEntities) { // 还原旧的后缀==> null
            k.setKindEntity(null);
            this.resourceSuffixRepository.save(k);
        }
    }


    /**
     * 获取全部可用(未选定)的后缀
     *
     * @return 后缀list
     */
    public List<SystemResourceSuffixEntity> findUsableAll() {
        return this.resourceSuffixRepository.findSystemResourceSuffixEntityByKindEntityIsNullOrderByCreateTimeDesc();
    }


    /**
     * 根据分类uuid 获取当前及可用的全部后缀
     *
     * @param uuid uuid
     * @return 后缀集
     */
    public List<SystemResourceSuffixEntity> findSystemResourceSuffixEntityByKindEntity_UuidOrKindEntityIsNull(UUID uuid) {
        return this.resourceSuffixRepository.findSystemResourceSuffixEntityByKindEntity_UuidOrKindEntityIsNullOrderByCreateTimeDesc(uuid);
    }

    /**
     * 根据uuid删除
     *
     * @param uuid uuid
     * @return 是否成功
     */
    public boolean delete(UUID uuid) {
        if (this.resourceSuffixRepository.existsById(uuid)) {
            SystemResourceSuffixEntity suffixEntity = this.resourceSuffixRepository.findById(uuid).get();
            if (null != suffixEntity.getKindEntity()) {
                throw new SystemExceptionHandler(ResultCode.DATA_FREE_MEM);
            }
            this.resourceSuffixRepository.delete(suffixEntity);
            return true;
        }
        return false;
    }
}
